package com.leancloud.circle.manage;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.leancloud.circle.bean.CircleBean;
import com.leancloud.circle.bean.CollectionBean;
import com.leancloud.circle.bean.CommentBean;
import com.leancloud.circle.bean.FansBean;
import com.scwang.smartrefresh.header.waterdrop.Circle;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.request.GetRequest;
import com.vise.xsnow.http.request.PostRequest;

import java.util.List;
import java.util.Locale;

import static tech.com.commoncore.constant.ApiConstant.BASE_URL;

public class CircleImp implements CircleInterface {
    private static CircleImp serverImp;

    public static CircleImp getInstance() {
        synchronized (CircleImp.class) {
            if (serverImp == null)
                serverImp = new CircleImp();
            return serverImp;
        }
    }

    @Override
    public void publishCircle(String token, String title, String keywords, String excerpt, String source, String content, String more, int category, int circleId, int topic, final CircleBack back) {
        PostRequest request = getPostRequest("/api/user/Circle/add_circle/", token)
                .addForm("post_title", title)
                .addForm("post_keywords", keywords)
                .addForm("post_excerpt", excerpt)
                .addForm("post_source", source)
                .addForm("post_content", content)
                .addForm("more", more)
                .addForm("category", category)
                .addForm("is_topic", topic);
        if (circleId != -1)
            request.addForm("id", circleId);
        request.request(new ACallback<String>() {
            @Override
            public void onSuccess(String data) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                if (jsonObject.getIntValue("code") == 1) {
                    handlerCircleBack(back, null, null);
                } else {
                    handlerCircleBack(back, null, jsonObject.getString("msg"));
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                handlerCircleBack(back, null, "网络错误");
            }
        });

    }

    @Override
    public void publishCircle(String token, String source, String content, String more, int category, int circleId, CircleBack back) {
        publishCircle(token, content, "", "", source, content, more, category, circleId, 0, back);
    }

    @Override
    public void removeCircle(String circleId, final CircleBack back) {
        ViseHttp.POST("/yapi/Circle/del_circle/").baseUrl(BASE_URL)
                .addForm("id", circleId)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject jsonObject = JSONObject.parseObject(data);
                        if (jsonObject.getIntValue("code") == 1) {
                            handlerCircleBack(back, null, null);
                        } else {
                            handlerCircleBack(back, null, jsonObject.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });

    }

    @Override
    public void requestCircle(int page, int num, String order, int category, int userId, int isTopic, String title, String applyName, final CircleBack back) {
        String language = Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
        GetRequest request = ViseHttp.GET("/new_api/Circle/index/").baseUrl(BASE_URL)
                .addHeader("xx-Language", language)
                .addParam("page", String.valueOf(page))
                .addParam("num", String.valueOf(num))
                .addParam("order", order)
                .addParam("is_topic", String.valueOf(isTopic));

        if (category != -1)
            request.addParam("category", String.valueOf(category));
        if (userId != -1)
            request.addParam("user_id", String.valueOf(userId));
        if (title != null && !title.isEmpty())
            request.addParam("title", title);
        if (applyName != null && !applyName.isEmpty())
            request.addParam("apply_name", applyName);
        request.request(new ACallback<String>() {
            @Override
            public void onSuccess(String data) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                if (jsonObject.getIntValue("code") == 1) {
                    JSONObject value = jsonObject.getJSONObject("data");
                    List<CircleBean> circleBeans = JSONArray.parseArray(value.getString("data"), CircleBean.class);
                    handlerCircleBack(back, circleBeans, null);
                } else {
                    handlerCircleBack(back, null, jsonObject.getString("msg"));
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                handlerCircleBack(back, null, errMsg);
            }
        });
    }

    @Override
    public void requestCircleDetails(int id, final CircleBack back) {
        ViseHttp.GET("/yapi/Circle/view/").baseUrl(BASE_URL)
                .addParam("id", String.valueOf(id))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject jsonObject = JSONObject.parseObject(data);
                        if (jsonObject.getIntValue("code") == 1) {
                            CircleBean bean = JSONObject.parseObject(jsonObject.getString("data"), CircleBean.class);
                            handlerCircleBack(back, bean, null);
                        } else {
                            handlerCircleBack(back, null, jsonObject.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }

    @Override
    public void commentCircle(String token, int circleId, String content, int commentId, final CircleBack back) {
        PostRequest request = getPostRequest("/api/user/Circle/add_comment/", token)
                .addForm("object_id", circleId)
                .addForm("content", content);
        if (commentId != -1)
            request.addForm("id", commentId);
        request.request(new ACallback<String>() {
            @Override
            public void onSuccess(String data) {
                JSONObject json = JSONObject.parseObject(data);
                if (json.getIntValue("code") == 1) {
                    handlerCircleBack(back, null, null);
                } else {
                    handlerCircleBack(back, null, json.getString("msg"));
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                handlerCircleBack(back, null, errMsg);
            }
        });
    }

    @Override
    public void likeCircle(String token, String circleId, final CircleBack back) {
        getPostRequest("/api/user/Circle/circle_like/", token)
                .addForm("object_id", circleId)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            handlerCircleBack(back, null, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });

    }

    @Override
    public void remorvLikeCircle(String token, String circleId, final CircleBack back) {
        getPostRequest("new_api/Circle/unsetZans", token)
                .addForm("object_id", circleId)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            handlerCircleBack(back, null, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }

    @Override
    public void likeComment(String token, int commentId, final CircleBack back) {
        getPostRequest("/api/user/Circle/comment_like/", token)
                .addForm("object_id", commentId)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            handlerCircleBack(back, null, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }

    @Override
    public void removeComment(int commentId, final CircleBack back) {
        ViseHttp.POST("/yapi/Circle/del_comment/").baseUrl(BASE_URL)
                .addForm("id", commentId)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            handlerCircleBack(back, null, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }

    @Override
    public void reportCircle(int circleId, final CircleBack back) {
        ViseHttp.GET("/api/user/Circle/report/").baseUrl(BASE_URL)
                .addParam("object_id", String.valueOf(circleId))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            handlerCircleBack(back, null, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }

    @Override
    public void requestCollection(String token, int page, String num, String order, final CircleBack back) {
        getGetRequest("/api/user/Circle/collection/", token)
                .addParam("page", String.valueOf(page))
                .addParam("num", String.valueOf(num))
                .addParam("order", order)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            JSONObject value = json.getJSONObject("data");
                            List<CollectionBean> collectionBeans = JSONArray.parseArray(value.getString("data"), CollectionBean.class);
                            handlerCircleBack(back, collectionBeans, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }

    @Override
    public void addCollection(String token, int circleId, final CircleBack back) {
        getPostRequest("/api/user/Circle/add_collection/", token)
                .addForm("object_id", circleId)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            handlerCircleBack(back, null, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }

    @Override
    public void removeCollection(String token, int circleId, final CircleBack back) {
        getGetRequest("/api/user/Circle/cancel_collection/", token)
                .addParam("id", String.valueOf(circleId))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            handlerCircleBack(back, null, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }


    @Override
    public void requestComment(int page, int num, String order, int circleId, int userId, final CircleBack back) {
        GetRequest request = ViseHttp.GET("/yapi/Circle/comment_list/").baseUrl(BASE_URL)
                .addParam("page", String.valueOf(page))
                .addParam("num", String.valueOf(num))
                .addParam("order", order);
        if (circleId != -1)
            request.addParam("object_id", String.valueOf(circleId));
        if (userId != -1)
            request.addParam("user_id", String.valueOf(userId));
        request.request(new ACallback<String>() {
            @Override
            public void onSuccess(String data) {
                JSONObject json = JSONObject.parseObject(data);
                if (json.getIntValue("code") == 1) {
                    JSONObject date = json.getJSONObject("data");
                    List<CommentBean> commentBeans = JSONArray.parseArray(date.getString("data"), CommentBean.class);
                    handlerCircleBack(back, commentBeans, null);
                } else {
                    handlerCircleBack(back, null, json.getString("msg"));
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                handlerCircleBack(back, null, errMsg);
            }
        });
    }

    @Override
    public void attentionAddOrRemove(String token, String followUserId, final CircleBack back) {
        getPostRequest("/yapi/circle/follow/", token)
                .addForm("followUserId", followUserId)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            handlerCircleBack(back, null, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }

    @Override
    public void requestCircleByAttention(String token, int page, final int num, String order, int category, int userId, int topic, String title, final CircleBack back) {
        GetRequest request = getGetRequest("/api/user/Circle/attention/", token)
                .addParam("page", String.valueOf(page))
                .addParam("num", String.valueOf(num))
                .addParam("order", order)
                .addParam("category", String.valueOf(category))
                .addParam("user_id", String.valueOf(userId))
                .addParam("is_topic", String.valueOf(topic));
        if (title != null)
            request.addParam("title", title);
        request.request(new ACallback<String>() {
            @Override
            public void onSuccess(String data) {
                JSONObject json = JSONObject.parseObject(data);
                if (json.getIntValue("code") == 1) {
                    JSONObject value = json.getJSONObject("date");
                    if (value != null) {
                        List<CircleBean> circleBeans = JSONArray.parseArray(value.getString("date"), CircleBean.class);
                        handlerCircleBack(back, circleBeans, null);
                    }
                    handlerCircleBack(back, null, null);
                } else {
                    handlerCircleBack(back, null, json.getString("msg"));
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                handlerCircleBack(back, null, errMsg);
            }
        });
    }

    @Override
    public void requestFans(final CircleBack back) {
        //TODO 这里没有传token,注意
        String token = "";
        getPostRequest("/yapi/user/userFollowedList/", token).baseUrl(BASE_URL)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            List<FansBean> fansBeans = JSONArray.parseArray(json.getString("data"), FansBean.class);
                            handlerCircleBack(back, fansBeans, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }

    @Override
    public void requestConcerned(final CircleBack back) {
        //TODO 这里没有传token,注意
        String token = "";
        getPostRequest("/yapi/user/userFollowList/", token).baseUrl(BASE_URL)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        JSONObject json = JSONObject.parseObject(data);
                        if (json.getIntValue("code") == 1) {
                            List<FansBean> fansBeans = JSONArray.parseArray(json.getString("data"), FansBean.class);
                            handlerCircleBack(back, fansBeans, null);
                        } else {
                            handlerCircleBack(back, null, json.getString("msg"));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        handlerCircleBack(back, null, errMsg);
                    }
                });
    }

    private synchronized void handlerCircleBack(CircleBack back, Object o, String err) {
        if (back != null) {
            back.onBack(o, err);
        }
    }

    private PostRequest getPostRequest(String url, String token) {
        return ViseHttp.POST(url).baseUrl(BASE_URL)
                .addHeader("XX-Token", token)
                .addHeader("XX-Device-Type", "android");
    }

    private GetRequest getGetRequest(String url, String token) {
        return ViseHttp.GET(url).baseUrl(BASE_URL)
                .addHeader("XX-Token", token)
                .addHeader("XX-Device-Type", "android");
    }
}
